package com.forstudy.util;

import com.forstudy.common.Constants;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: erdong
 * Date: 2020/4/15
 * Time: 21:32
 * Description: No Description
 */

public class PoiUtil {

    private final static String excel2003L = ".xls";
    private final static String excel2007U = ".xlsx";

    public static List<String> queryTopCell(String tag, String fileName, String caseCode) {

        Workbook workbook = null;
        //sheet页name
        Sheet sheet = null;
        String SheetName="";
        Row row = null;
        Cell cell = null;//验证文件格式
        try {
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            InputStream is = new FileInputStream(new File(fileName));
            if (suffix.equals(excel2003L)) {
                workbook = new HSSFWorkbook(is);
            } else if (suffix.equals(excel2007U)) {
                workbook = new XSSFWorkbook(is);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Error ee) {
            ee.printStackTrace();
        } finally {
            System.out.print("");
        }

        List<String> cellList= new ArrayList<String>();
        //只取第一个sheet页
        for (int i = 0; i < 1; i++) { //workbook.getNumberOfSheets()
            //获取第一个sheet
            sheet = workbook.getSheetAt(i);
            if (sheet == null) {
                continue;
            }
            SheetName=sheet.getSheetName();

/*            cellList.add(caseCode); //0
            cellList.add(fileName); //1
            cellList.add(SheetName); //2*/
            //获取第一行
            row=sheet.getRow(0);
            //遍历第一行

            int o =0;
            Iterator<Cell> it=row.iterator();
            while(it.hasNext())
            {
                cell=it.next();
                o++;
                if (cell == null) {
                    continue;
                }
                String c=String.valueOf(cell);
                System.out.println("第"+o+"个:"+c);
                cellList.add(c);
            }
            int rowNum = sheet.getLastRowNum();
            List<List<String>> datas = new ArrayList<>();
            for(int j=1; j<=rowNum; j++) {
                List<String> data = new ArrayList<>();
                row=sheet.getRow(j);
                it=row.iterator();
                o=0;
                while(it.hasNext())
                {
                    cell=it.next();
                    o++;
                    String c = "";
                    if (cell != null) {
                        c =String.valueOf(cell);
                    }
                    data.add(c);
                }
                datas.add(data);
            }
            Constants.datas.put(tag, datas);
        }
        return cellList;
    }

    public static void main(String[] args) throws Exception {

        String path = "D:\\test.xlsx";
        queryTopCell("123", path, "sheet1");
    }
}
